package controller;

import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import service.LogService;
import service.UserService;
import utils.MyLogger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by MesakiiYui on 2018/11/11.
 */

@Controller
@RequestMapping(value = "/main", produces = "text/html;charset=utf-8")
public class SystemController {

    @Autowired
    private UserService userService;

    @Autowired
    private LogService logService;

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String getLogin(HttpServletRequest request, HttpSession session) {
        return "login";
    }

    // 病人病历管理
    @RequestMapping(value = "/medicalRecords", method = RequestMethod.GET)
    public String getMedicalRecords(HttpServletRequest request, HttpSession session) {
        return selector(request, session, "medicalRecords", "login");
    }


    // 病人数据统计分析管理
    @RequestMapping(value = "/medicalRecordsAnalysis", method = RequestMethod.GET)
    public String getMedicalRecordsAnalysis(HttpServletRequest request, HttpSession session) {
        return selector(request, session, "medicalRecordsAnalysis", "login");
    }

    // 成像数据管理
    @RequestMapping(value = "/imagingData", method = RequestMethod.GET)
    public String getImagingData(HttpServletRequest request, HttpSession session) {
        return selector(request, session, "imagingData", "login");
    }

    // 项目管理
    @RequestMapping(value = "/projectManage", method = RequestMethod.GET)
    public String getProjectManage(HttpServletRequest request, HttpSession session) {
        return selector(request, session, "projectManage", "login");
    }

    // 评价体系管理
    @RequestMapping(value = "/commentManage", method = RequestMethod.GET)
    public String getCommenttManage(HttpServletRequest request, HttpSession session) {
        return selector(request, session, "commentManage", "login");
    }

    // 平台日志信息管理
    @RequestMapping(value = "/logManage", method = RequestMethod.GET)
    public String getLogManage(HttpServletRequest request, HttpSession session) {
        return selector(request, session, "logManage", "login");
    }

    //平台耗材管理
    @RequestMapping(value = "/consumableMaterial", method = RequestMethod.GET)
    public String getConsumableMaterial(HttpServletRequest request, HttpSession session) {
        return selector(request, session, "consumableMaterial", "login");
    }

    //平台用户及权限管理
    @RequestMapping(value = "/userManage", method = RequestMethod.GET)
    public String getUserManage(HttpServletRequest request, HttpSession session) {
        if (session.getAttribute("cUser") == null) {
            return "login";
        } else {
            User user = (User) session.getAttribute("cUser");
            if (user.getRole() == 1) {
                new MyLogger().addLog(logService, user.getUser(), df.format(new Date()), request.getRequestURL().toString());
                return "userManage";
            } else {
                return "platformPortal";
            }
        }
    }


    // 超声波成像系统平台门户
    @RequestMapping(value = "/platformPortal", method = RequestMethod.GET)
    public String getPlatformPortal(HttpServletRequest request, HttpSession session) {
        return selector(request, session, "platformPortal", "login");
    }

    // 多通道发射控制
    @RequestMapping(value = "/sendControl", method = RequestMethod.GET)
    public String getSendControl(HttpServletRequest request, HttpSession session) {
        return selector(request, session, "sendControl", "login");
    }

    //多通道接收控制
    @RequestMapping(value = "/receiveControl", method = RequestMethod.GET)
    public String getReceiveControl(HttpServletRequest request, HttpSession session) {
        return selector(request, session, "receiveControl", "login");
    }

    //外部同步控制
    @RequestMapping(value = "/syncControl", method = RequestMethod.GET)
    public String getSyncControl(HttpServletRequest request, HttpSession session) {
        return selector(request, session, "syncControl", "login");
    }

    //数据采集与重建
    @RequestMapping(value = "/dataCollect", method = RequestMethod.GET)
    public String getDataCollect(HttpServletRequest request, HttpSession session) {
        return selector(request, session, "dataCollect", "login");
    }

    //任意波束形成
    @RequestMapping(value = "/waveGenerate", method = RequestMethod.GET)
    public String getWaveGenerate(HttpServletRequest request, HttpSession session) {
        return selector(request, session, "waveGenerate", "login");
    }

    //超声探头脚本管理
    @RequestMapping(value = "/scriptManage", method = RequestMethod.GET)
    public String getScriptManage(HttpServletRequest request, HttpSession session) {
        return selector(request, session, "scriptManage", "login");
    }

    //成像模式算法
    @RequestMapping(value = "/createImgAlgorithm", method = RequestMethod.GET)
    public String getCreateImgAlgorithm(HttpServletRequest request, HttpSession session) {
        return selector(request, session, "createImgAlgorithm", "login");
    }

    //图像处理算法
    @RequestMapping(value = "/resolveImgAlgorithm", method = RequestMethod.GET)
    public String getResolveImgAlgorithm(HttpServletRequest request, HttpSession session) {
        return selector(request, session, "resolveImgAlgorithm", "login");
    }

    //病人信息管理
    @RequestMapping(value = "/patientManage", method = RequestMethod.GET)
    public String getPatientManage(HttpServletRequest request, HttpSession session) {
        return selector(request, session, "patientManage", "login");
    }

    // 登陆页面
    @RequestMapping(value = "/login.do", method = RequestMethod.POST)
    public String submitLogin(HttpServletRequest request, HttpSession session) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        User cUser = userService.valid(username, password);
        if (cUser != null) {
            System.out.println(cUser.getUser());
            session.setAttribute("cUser", cUser);
            return "platformPortal";
        } else {
            return "login";
        }
    }

    private String selector(HttpServletRequest request, HttpSession session, String successPage, String failPage) {
        if (session.getAttribute("cUser") == null) {
            return failPage;
        } else {
            User user = (User) session.getAttribute("cUser");
            new MyLogger().addLog(logService, user.getUser(), df.format(new Date()), request.getRequestURL().toString());
            return successPage;
        }
    }


}
